#!/usr/bin/env python

from setuptools import setup, Extension, find_packages
import numpy as np #needed for numpy include paths
import glob
import os
import sys
from Cython.Distutils import build_ext
import Cython
from distutils.version import StrictVersion

# make sure cython isn't too old
if StrictVersion(Cython.__version__) < StrictVersion('0.26'):
    sys.exit("Cython version is too old. Please update using pip install cython --upgrade [--user]")
    
# setup moab include paths
moab_root = '@abs_top_srcdir@'
moab_source_include = moab_root + '/src/moab/'
moab_other_source_include = moab_root + '/src/'
moab_binary_include = '@abs_top_builddir@/src/'
moab_lib_path = '@abs_top_builddir@/src/.libs/'
pymoab_src_dir = '@abs_builddir@/pymoab'
include_paths = [moab_source_include,
                 moab_other_source_include,
                 moab_binary_include,
                 np.get_include(),
                 pymoab_src_dir]


os.environ["CC"] = "@CC@"
os.environ["CXX"] = "@CXX@"

if "--preinstall" in sys.argv:
    moab_rpath = '@prefix@/lib'
    sys.argv.remove("--preinstall")
else:
    moab_rpath = moab_lib_path

# set values for each module
ext_modules = []
for f in os.listdir(pymoab_src_dir):
    if f.endswith(".pyx"):
        fbase = f.split('.')[0]
        ext = Extension( "pymoab."+fbase, ["pymoab/"+f,],
                         language = 'c++',
                         include_dirs = include_paths,
                         runtime_library_dirs = [moab_rpath,],
                         library_dirs = [moab_lib_path,],
                         libraries = ["MOAB",])
        ext_modules.append(ext)
        
# setup pymoab
setup(
    name="pymoab",
    cmdclass = {'build_ext': build_ext},
    ext_modules=ext_modules,
    packages=find_packages(),
    package_data = {'pymoab': ['*.pxd',]},
    version = '@VERSION@',
    author = "Patrick Shriwise, Guilherme Caminha, Vijay Mahadevan, Iulian Grindeanu, Anthony Scopatz",
    author_email = "moab-dev@mcs.anl.gov"
)
